Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

nice_things/utility/realpath.sh

realpath

Since 0.3.0 · Source

import "{ realpath }" from nice_things/utility/realpath.sh

Synopsis
realpath [-E|-e] [--] <pathname>

Configuration

Description
Print the resolved absolute path name; all but the last component must exist, unless the -e option was used, in which case all pathname components must exist.

This is a sh implementation of the realpath utility specified in POSIX. This implementation tries to closely emulate the default behavior of the realpath utilities in Busybox and GNU coreutils, which was standardized in POSIX-2024.

Being an implementation of a CLI utility, this function prints the result to stdout. If you want to assign the result to a variable, consider using the faster canonicalize function instead.

Note

This is not a pure sh implementation because it depends on the external ls utility to read the target of a symlink. This works consistently because the output of ls is strictly specified in POSIX, but it means this function can be slower than most other functions in the framework. The slow branch is only reached when the last component of <pathname> is a symlink.

Options

  • -E: Succeed if the last pathname component does not exist; this is the default behavior.
  • -e: Fail if the last pathname component does not exist.
  • --: End of options.

Operands
<pathname>: A path name.

Stdin

Stdout
The result is printed to stdout, trailed by a line-feed character.

Stderr
log_debug, log_error.

Exit status

  • 0: Successful completion.
  • 23: No such file or directory.
  • 28: Too many levels of symbolic links.

Abort

Usage examples

# Print the real path to the user's home directory
realpath "$HOME"